from random import randint

n = randint(1, 8)

src = []

for i in range(n):
    src.append(randint(1, 1024))

print(src)
numbers = src[:]


def sink(begin):
    if begin == n-1:
        return
    sink(begin+1)
    swap(begin, begin+1)


def swap(i, k):
    if numbers[i] > numbers[k]:
        force_swap(i, k)


def force_swap(i, k):
    if i == k:
        return
    print("%d <-> %d" % (numbers[i], numbers[k]), end=', ')
    tmp = numbers[i]
    numbers[i] = numbers[k]
    numbers[k] = tmp


def descend(x, y): return 1 if x > y else 0


def ascend(x, y): return 1 if x < y else 0


def least(begin):
    return select(begin, ascend)


# pretend order [begin: len-1] by f then return the first element
def select(begin, f):
    if begin > n-2:
        return n-1
    idx = select(begin+1, f)
    if f(numbers[begin], numbers[idx]):
        return begin
    return idx


def biggest(begin):
    return select(begin, descend)


def least_sort():
    for i in range(0, n):
        idx = least(i+1)
        force_swap(i, idx)
    print("\n{}".format(numbers))


def select_sort(ordered):
    # pdb.set_trace()
    for i in range(0, n):
        idx = select(i+1, ordered)
        if ordered(numbers[i], numbers[idx]):
            continue
        force_swap(i, idx)
    print("\n{}".format(numbers))


def sink_sort():
    for i in range(0, n):
        sink(i)
    print("\n{}".format(numbers))


# least_sort()
select_sort(descend)
select_sort(ascend)

# numbers = src[:]
# sink_sort()

'''
idx = biggest(0)
force_swap(0, idx)
print(f"\n{numbers}")
idx = least(0)
force_swap(len(numbers)-1, idx)
print(f"\n{numbers}")
'''
